<?php 
/**
* 
*/
class Guoyou
{
    // 配置文件
    private  $key;
    private  $callback;
    private  $notify_url;
    public function __construct()
    {
            $class = strtolower("Guoyou");
            // 加载
            $this->Config  = $Config = Config::get($class);
            $this->Account = $Config['Account'];
            $this->Syscode = $Config['Syscode'];
            $this->key = $Config['key'];
            
            // 公共参数
            $this->Version = "002";
            $this->PayCode   = "100008";
	$this->QueryCode = "100009";
	$this->API_GATEWAY = "API_GATEWAY";
            $this->API_QWAB = "API_QWAB";
	$this->Pay_server = "http://www.yitianmao.com/cgi-bin/gateway_pay_pho.cgi"; 
	$this->Query_server =  "http://www.yitianmao.com/cgi-bin/get_result_m.cgi";  


		// 获取主机记录 www.test.cn/
            $domain = Request::instance()->domain();
            //curl实例
            $this->http =new \http\Http();

            // 成功后的回调与业务逻辑            
            // 以前的方法和后续处理
            
            // 需要协助开始看着改
            
            if(!empty($Config['callback_url'])){
                $this->callback = $Config['callback_url'];
            }else{
                $this->callback = $domain."/out/Notify/callbackurl";
            }
            if(!empty($Config['notify_url'])){
                $this->notify_url = $Config['notify_url'];
            }else{
                $this->notify_url =$domain."/out/Notify/handll";
            }
            
            // 需要协助结束
    }


    // 生成支付payurl
    // 网关支付
    public function byPay($deposit,$payNo,$CertName,$CertNo,$Phone){
                // 基础参数处理
                // $baseData = $this->baseData();
                // $payData = $this->payData($deposit,$payNo,$CertName,$CertNo,$Phone);//风控基本参数
                // $riskItemData = $this->riskItemData($CertName,$CertNo,$Phone);//风控基本参数
                // $payData = array_merge($payData,$baseData);
                // $payData['risk_item'] = json_encode($riskItemData,JSON_UNESCAPED_UNICODE);
                // $payData = paraFilter($payData);
                // //对待签名参数数组排序
                // $payData = argSort($payData);
                // 
                // //生成签名结果
                // $mysign = $this->buildRequestMysign($payData);
                // //签名结果与签名方式加入请求提交参数组中
                // $payData['sign'] = $mysign;
                // $res =  $this->buildRequestForm(json_encode($payData,JSON_UNESCAPED_UNICODE));

        // 业务逻辑开始 
        
        // 生成请求报文Context
        date_default_timezone_set('PRC'); 
        header("ontent-Type:text/html; charset=UTF-8");
        $data = array(
            'server_no' => $this->PayCode,
            'trans_time' => date("YmdHis"),// 
            'account' => $this->Account,// 
            'amount' => "1",// 固定充值金额1分  $deposit
            'pay_mode'=>$$this->API_QWAB ,//$API_GATEWAY,
        );
        // $key为空也可以传
        $Signstr = "{" . $data["server_no"] . "}|{" . $data["trans_time"] . "}|{" .$data["account"] . "}|{" . $data["amount"] . "}|{" . $data["pay_mode"] . "}|{" . $key . "}";
        // 加密sign
        $Sign = substr(strtoupper(md5($Signstr)),0,16);

        // data参数生成Context报文头
        $data['aging']= '2' ; //t0=1 t1=2
        $data['app_id']=date("YmdHis");    //商户流水号 $payNo

        // 回调地址
        // $data['callback_url']=$this->callback;
        $data['callback_url']='http://www.hello.comm/success.htm';
        // 异步通知地址
        // $data['notify_url']=$this->notify_url;
        $data['notify_url']='http://www.hello.com/norify.cgi';
        $data['memo']='千领';
 

        $Context = "server_no=" . $data["server_no"]. "&trans_time=" . $data["trans_time"] . "&account=" . $data["account"] . "&amount=" . $data["amount"] . "&pay_mode=" . $data["pay_mode"] .
            "&aging=" . $data["aging"] . "&app_id=" . $data["app_id"] . "&callback_url=" . $data["callback_url"] . "&notify_url=" . $data["notify_url"] . "&memo=" . $data["memo"];
        $Context=mb_convert_encoding($Context, "GBK","UTF-8");
        // 请求报文 Context
        $Context=base64_encode($Context);

        $Context = $this->PackUrlBase64($Context);
        $strUrl=$this->Postdata($this->Syscode,$this->Version,$Context,$Sign);
        
        list ( $curl_errno, $result )=$this->Post($Pay_server,$strUrl);
   
        if ( $curl_errno >0) { 
            echo '通讯失败 errorno: '.$curl_errno;
            return ;
        }   
        $dic=json_decode($result,true);
         
        if ($dic["errorcode"] != "0000"){           
            echo ("\n请求失败!" . $dic["errorcode"] ."  ". $dic["errormessage"] );
            return;
        }
        $strSign = $dic["signature"];
        $context = $this->UnPackUrlBase64($dic["context"]);           
        $context = base64_decode($context);
        // echo $context;
        parse_str($context,$dic); 
        // var_dump($dic);
        
        $Signstr = "{" . $dic["trans_id"] . "}|{" . $dic["amount"] . "}|{" . $dic["pay_url"] . "}|{" . $key . "}";
        $Sign = substr(strtoupper(md5($Signstr)),0,16);
        if ($strSign != $Sign)
        {
            echo("\n签名失败! Get：" . $strSign." Sign: ".$Sign);
            return;
        }
         $pay_url =   $this->UnPackUrlBase64( $dic["pay_url"] );
         $res =   base64_decode( $pay_url ) ;

         // $res 就是支付的url


        // 业务逻辑结束

        if(getOS()=="Linux"){
            error_log($res.PHP_EOL, 3, "/home/wwwlogs/guoyou_dayingjia_form.log");
        }
        if(!empty($res)){
            return $res;
        }
        return false;
    }

    // 本代码中的post请求
    public function  Post($Url,$strUrl)
    {    
        $strUrl = mb_convert_encoding($strUrl, "GBK","UTF-8");  
        $ch = curl_init();
        $this_header = array(
            "content-type: application/x-www-form-urlencoded; charset=GBK");
        curl_setopt($ch, CURLOPT_URL, $Url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $strUrl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        $result=curl_exec($ch);   
        $result =  mb_convert_encoding($result,"UTF-8", "GBK"); 
       
        $curl_errno = curl_errno($ch);
        curl_close($ch);
         
        return  array($curl_errno,$result);          
 
    }

    // 整理post数据
    public function  Postdata($Syscode,$Version,$result,$Sign)
    {    
        $data=array(
                 "syscode"=>$Syscode,
                 "version"=>$Version,
                 "context" => $result,
                 "signature"=> $Sign,
        );

        $strUrl = "syscode=" . $data["syscode"] . "&version=" . $data["version"] . "&context=" . $data["context"] . "&signature=" . $data["signature"] ; 
        $strUrl = mb_convert_encoding($strUrl, "GBK","UTF-8");  
        return  $strUrl ;
 
    }   

    // 打包base64
    public function PackUrlBase64($strdata) {
        $strdata = str_ireplace("+","-",$strdata);
        $strdata = str_ireplace("/","_",$strdata);
        $strdata = str_ireplace("=","",$strdata);
        return $strdata;
     }

     // 拆包base64
     public function UnPackUrlBase64($strdata) {
        $strdata = str_ireplace("-","+",$strdata);
        $strdata = str_ireplace("_","/",$strdata);
       while (strlen($strdata) % 4 != 0)
        {
             $strdata .= "=";
        }       
  
        return $strdata;
     }


	
}

















 ?>